package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.mihuyou.entity.Lefimg;
import com.yzc.mihuyou.entity.Lefwue;
import com.yzc.mihuyou.entity.Popup;
import com.yzc.mihuyou.mapper.LefImgMapper;
import com.yzc.mihuyou.mapper.LefWueMapper;
import com.yzc.mihuyou.mapper.PopupMapper;
import com.yzc.mihuyou.service.PopupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author yzc
 * @since 2022-05-16
 */
@Service
public class PopupServiceImpl extends ServiceImpl<PopupMapper, Popup> implements PopupService {

    @Autowired
    PopupMapper popupMapper;
    @Autowired
    LefImgMapper lefImgMapper;
    @Autowired
    LefWueMapper lefWueMapper;
    @Autowired
    UserService userService;

    @Override
    public Map<String, Object> addSele(Popup popup) {
        if(popup.getPoupId()!=null){
           Popup popup1=popupMapper.selectById(popup.getPoupId());
            List<String> result = Arrays.asList(popup1.getUserId().split(","));
            for (int i=0;i<result.size();i++){
                if (result.get(i).equals(popup.getUserId())){
                    popup1.setUserId(popup1.getUserId()+","+popup.getUserId());
                }
            }
            popupMapper.updateById(popup);
        }else {
            popupMapper.insert(popup);
        }
        return null;
    }

    @Override
    public List selSeke(int id) {
        QueryWrapper<Popup> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("lef_id",id);
        List<Popup> list=popupMapper.selectList(queryWrapper);

        Lefwue lefWue=lefWueMapper.selectById(id);

        Lefimg lefImg=lefImgMapper.selectById(lefWue.getLeafletId());

        List poup=new ArrayList();
        for(Popup popup:list){
            Map<String,Object> map=new HashMap<>();
            map.put("popup",popup);
            map.put("lefWue",lefWue);
            map.put("lefImg",lefImg);
            List<String> result = Arrays.asList(popup.getUserId().split(","));
            List list1=new ArrayList<>();
            for (String userid:result){
                list1.add(list1.size(),userService.cuw(Long.valueOf(userid)));
            }
            map.put("user",list1)   ;
            poup.add(poup.size(),map);
        }
        return poup;
    }
}
